package com.example.demo.service.impl;/**
 * @author Daog
 * @create 2020 -11 -2020-11-02 22:24
 * @description 功能描述
 */

import com.example.demo.service.UserService;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: demo_boot
 *
 * @description:
 *
 * @author: Dong
 *
 * @create: 2020-11-02 22:24
 **/
public class UserServiceImpl implements UserService {

    @Autowired
    JdbcTemplate jdbcTemplate;


    @Override
    public List<User> getUserList() {
        return jdbcTemplate.query("select*from user",new BeanPropertyRowMapper(User.class));
    }

    @Override
    public void createUser(User user) {

        jdbcTemplate.update("insert into user (name ,age)values (?,?)",user.getName(),user.getAge());

    }

    @Override
    public User getUser(Long id) {
        return (User) jdbcTemplate.queryForObject("select*from user where id=?",new BeanPropertyRowMapper(User.class),id);
    }

    @Override
    public void updateUser(Long id, User user) {
        jdbcTemplate.update("update users set name=?,age=? where id=?",user.getName(),user.getAge(),id);
    }

    @Override
    public void deleatUser(Long id) {
        jdbcTemplate.update("delete from users where id=?",id);
    }
}
